package com.chushouya.manager.service.admin.impl;

import com.chushouya.common.support.Contexts;
import com.chushouya.manager.dto.admin.bannerposition.*;
import com.general.framework.core.lang.Beans;
import com.general.framework.core.lang.Numbers;
import com.general.framework.core.lang.Validate;
import com.general.framework.core.schema.PageResult;
import com.chushouya.manager.dao.entity.BannerPositionEntity;
import com.chushouya.manager.dao.repository.BannerPositionRepository;
//=====manager.dto.admin.bannerposition.*;
import com.chushouya.manager.dto.admin.bannerposition.*;
import com.chushouya.manager.service.admin.BannerPositionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class BannerPositionServiceImpl implements BannerPositionService {

    @Resource
    private BannerPositionRepository positionRepository;

    @Override
    public void save(BannerPositionSaveDTO dto) {
        BannerPositionEntity banner = Beans.copy(dto, BannerPositionEntity.class);
        banner.setCreateTime(new Date());
        banner.setHandleUser(Contexts.getUserContext().getUserName());
        positionRepository.insertSelective(banner);
    }

    @Override
    public void update(BannerPositionUpdateDTO dto) {
        BannerPositionEntity banner = positionRepository.selectByPrimaryKey(dto.getPositionId());
        Validate.isNull(banner, "banner不存在");
        BannerPositionEntity update = Beans.copy(dto, BannerPositionEntity.class);
        update.setUpdateTime(new Date());
        update.setHandleUser(Contexts.getUserContext().getUserName());
        positionRepository.updateByPrimaryKeySelective(update);
    }

    @Override
    public void remove(Long positionId) {
        Validate.isNull(positionId, "positionId不能为空");
        BannerPositionEntity update = new BannerPositionEntity();
        update.setPositionId(positionId);
        update.setDelFlag(Numbers.INTEGER_ONE);
        update.setUpdateTime(new Date());
        update.setHandleUser(Contexts.getUserContext().getUserName());
        positionRepository.updateByPrimaryKeySelective(update);
    }

    @Override
    public PageResult<BannerPositionListDTO> selectPage(BannerPositionQuery query) {
        PageResult<BannerPositionEntity> page = positionRepository.selectPage(query);
        return PageResult.of(page, BannerPositionListDTO.class);
    }

    @Override
    public BannerPositionDetailDTO detail(Long positionId) {
        Validate.isNull(positionId, "positionId不能为空");
        BannerPositionEntity banner = positionRepository.selectByPrimaryKey(positionId);
        Validate.isNull(banner, "position不存在");
        return Beans.copy(banner, BannerPositionDetailDTO.class);
    }

    @Override
    public List<BannerPositionListDTO> selectList(BannerPositionQuery query) {
        List<BannerPositionEntity> list = positionRepository.selectList(query);
        return Beans.copyList(list, BannerPositionListDTO.class);
    }

}
